package com.practice.communication.nio;

import java.io.IOException;

public class Client {
	
	private static String CLIEN_DEFAULT_IP = "127.0.0.1";
	
	private static int CLIENT_DEFAULT_PORT = 8080;
	
	private static ClientHandler clientHandler;
	
	public static void start() {
		start(CLIEN_DEFAULT_IP, CLIENT_DEFAULT_PORT);
	}
	
	public static synchronized void start(String ip, int port) {
		if (clientHandler != null) 
			clientHandler.stop();
		
		clientHandler = new ClientHandler(ip, port);
		new Thread(clientHandler, "Client").start();
	}
	
	public static boolean sendMsg(String msg) throws IOException {
		if ("q".equals(msg)) return false;
		
		clientHandler.sendMsg(msg);
		return true;
	}
	
	public static void main(String[] args) {
		start();
	}

}
